/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.client.edit;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import dk.lindhardt.gwt.geie.shared.Cell;
import dk.lindhardt.gwt.geie.shared.DateCell;
import dk.lindhardt.gwt.geie.shared.Formula;
import dk.lindhardt.gwt.geie.shared.TZIgnoringDate;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * User: AnAmuser
 * Date: 30-07-11
 * <p/>
 * Handles edit of date cells
 */
public class DateValueEditor implements ValueEditor {

   private Cell cell;
   private DateBox editBox;
   private String pattern;
   private EditHandler editHandler;

   public DateValueEditor(EditHandler editHandler, Cell cell, String pattern) {
      this.editHandler = editHandler;
      this.cell = cell;
      this.pattern = pattern;
      editBox = new DateBox();
   }

   public boolean isListeningForTableSelections() {
      return false;
   }

   public void handleTableSelection(List<Cell> cells) {
      //Intentionally left empty
   }

   public Cell createNewCell() {
      return new DateCell(editBox.getValue(), cell.getPosition(), cell.getCellFormatKey());
   }

   public boolean validate() {
      return editBox.getValue() != null;
   }

   public Widget getInputWidget() {
      editBox.addDomHandler(
            new KeyDownHandler() {
               public void onKeyDown(KeyDownEvent event) {
                  if (editBox.getValue() != null) {
                     int code = event.getNativeKeyCode();
                     if (KeyCodes.KEY_ENTER == code) {
                        editHandler.onEditConfirmed();
                     }
                  }
               }
            }, KeyDownEvent.getType());
      return editBox;
   }

   public void setInitialInputValue(Serializable value) {
      if (value instanceof String) {
         editBox.getTextBox().setText((String) value);
      } else if (value instanceof Double) {
         editBox.getTextBox().setText(String.valueOf(value));
      } else if (value instanceof Boolean) {
         editBox.getTextBox().setText(String.valueOf(value));
      } else if (value instanceof Date) {
         editBox.setValue((Date) value);
      } else if (value instanceof TZIgnoringDate) {
         editBox.setValue(((TZIgnoringDate) value).getDate());
      } else if (value instanceof Formula) {
         editBox.getTextBox().setText(((Formula) value).getFormula());
      }
   }
}
